package com.site.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.function.Predicate;


/**
 * swagger配置
 */
@Configuration
@EnableSwagger2WebMvc
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {

    @Value("${swagger.basePackage}")
    private String basePackage;
    @Value("${swagger.enable}")
    private boolean enable;
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.description}")
    private String description;
    @Value("${swagger.version}")
    private String version;
    @Value("${swagger.address}")
    private String address;
    @Value("${swagger.name}")
    private String name;
    @Value("${swagger.email}")
    private String email;

    @Bean
    public Docket createRestApi(){
        // 用于测试接口添加头部信息
        ArrayList<Parameter> parameters = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();// 多个参数直接 new ParameterBuilder()
        parameterBuilder.name("Authorization").description("令牌参数(必须)").modelRef(new ModelRef("String")).parameterType("header").required(true);
        parameters.add(parameterBuilder.build());
        Predicate<RequestHandler> basePackages = RequestHandlerSelectors.basePackage(basePackage);
        // 多个服务，添加多个
        //Predicate<RequestHandler> basePackages1 = RequestHandlerSelectors.basePackage("com.bale.user.controller");
        //Predicate<RequestHandler> basePackages2 = RequestHandlerSelectors.basePackage("com.bale.file.controller");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(basePackages)
                //.apis(basePackages1)
                //.apis(basePackages2)
                .paths(PathSelectors.any())// 所有 controller
                .build()
                .globalOperationParameters(parameters)
                .enable(enable);
    }

    /**
     * api文档信息
     * @return
     */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .contact(new Contact(name,address,email))
                .build();
    }
}
